#!/user/bin/env node
const https = require('https'),
      http = require('http'),
      log = console.log,
      cheerio = require('cheerio'),
      fs = require('fs');
let newsList = [];

http.createServer((req,res)=>{
  log('1');
  let html = fs.readFileSync('./news.html').toString('utf8');
  if(req.url==='/'){
    log('2')
    let $ = cheerio.load(html);
    let list = $('body').find('div.news-model');
    for(let i=0;i<list.length;i++){
      //log($(list[i]));
      let id1 = $(list[i]).find('a').text();
      if(id1.indexOf('2018')!= '-1'){
        let id2 = $(list[i]).find('span').text();
        let obj ={};
        obj.newstitle = id1;
        obj.nestime = id2;
        newsList.push(obj);
      }
    }
  }
    log(newsList,'newsList');
    res.end(JSON.stringify(newsList));
}).listen(8081);




